/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_brand.service;


import com.yuecai.modules.yc_brand.domain.YcBrand;
import com.yuecai.modules.yc_brand.repository.YcBrandRepository;
import com.yuecai.modules.yc_brand.service.dto.YcBrandDto;
import com.yuecai.modules.yc_brand.service.dto.YcBrandQueryCriteria;
import com.yuecai.modules.yc_brand.service.mapstruct.YcBrandMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-06-29
**/
@Service
@RequiredArgsConstructor
public class YcBrandService {

    private final YcBrandRepository ycBrandRepository;
    private final YcBrandMapper ycBrandMapper;

    
    public Map<String,Object> queryAll(YcBrandQueryCriteria criteria, Pageable pageable){
        Page<YcBrand> page = ycBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycBrandMapper::toDto));
    }

    
    public List<YcBrandDto> queryAll(YcBrandQueryCriteria criteria){
        return ycBrandMapper.toDto(ycBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcBrandDto findById(Long id) {
        YcBrand ycBrand = ycBrandRepository.findById(id).orElseGet(YcBrand::new);
        ValidationUtil.isNull(ycBrand.getId(),"YcBrand","id",id);
        return ycBrandMapper.toDto(ycBrand);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcBrandDto create(YcBrand resources) {
        return ycBrandMapper.toDto(ycBrandRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcBrand resources) {
        YcBrand ycBrand = ycBrandRepository.findById(resources.getId()).orElseGet(YcBrand::new);
        ValidationUtil.isNull( ycBrand.getId(),"YcBrand","id",resources.getId());
        ycBrand.copy(resources);
        ycBrandRepository.save(ycBrand);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycBrandRepository.deleteById(id);
        }
    }

    
    public void download(List<YcBrandDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcBrandDto ycBrand : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("品牌名称", ycBrand.getBrandName());
            map.put("品牌图标 url", ycBrand.getBrandIcon());
            map.put("品牌banner url", ycBrand.getBackgroundImage());
            map.put("品牌详情（简介）", ycBrand.getDetailInfo());
            map.put("是否展示 1是 0否", ycBrand.getIsShow());
            map.put("品牌描述", ycBrand.getBrandTitle());
            map.put("排序", ycBrand.getOrderSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<YcBrand> findByName(String name) {
        List<YcBrand> list = ycBrandRepository.findByBrandNameLikeAndIsShowTrueOrderByOrderSort(name);
        if (list.size()>5){
            List<YcBrand> newList = list.subList(0, 5);//取前四条数据
            return newList;//返回新的list
        }
        return list;
    }
}